import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'
import actions from './actions'

Vue.use(Vuex)

const store = new Vuex.Store({
  strict: process.env.NODE_ENV === 'development',
  state,
  mutations,
  getters,
  actions
})

store.subscribe((mutation, state) => {
  // 订阅所有的mutation,只要有任意一个mutation被commit,都会出发这个回调
  if (mutation.type === 'toggleIslogin') {
    // 用户信息被修改了
    window.localStorage.setItem('login', JSON.stringify(state.userinfo))
  }
})

export default store
